package com.goldenautumn.prefabricate.activity.mine.order

import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.databinding.ActivityOrderListBinding
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_AFTER_SALES
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_ALL
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_COMPLETE
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_PENDING
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_RECEIVING
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_SHIPMENT
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.isActive
import qncx.external.common.base.BaseActivity
import qncx.external.common.widget.tabLayout.ViewPager2Adapter
import qncx.external.common.widget.tabLayout.listener.OnTabSelectListener

class OrderListActivity : BaseActivity<ActivityOrderListBinding>() {

    companion object {
        fun start(context: Context, type: Int) {
            val intent = Intent(context, OrderListActivity::class.java)
            intent.putExtra(BUSINESS_PAGE_DATA, type)
            context.startActivity(intent)
        }
    }

    private val orderType: Int?
        get() = intent?.getIntExtra(BUSINESS_PAGE_DATA,0)

    override fun getLayoutId(): Int = R.layout.activity_order_list

    override suspend fun main() {
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        initToolbarNav(mBinding.toolbar)

        var titles = arrayListOf(getString(R.string.order_status_all),getString(R.string.order_status_pending),getString(R.string.order_status_shipment),getString(R.string.order_status_receiving),getString(R.string.order_status_complete))
        var fragment = ArrayList<Fragment>()
        fragment.add(OrderListFragment.newInstance(ORDER_TYPE_ALL))
        fragment.add(OrderListFragment.newInstance(ORDER_TYPE_PENDING))
        fragment.add(OrderListFragment.newInstance(ORDER_TYPE_SHIPMENT))
        fragment.add(OrderListFragment.newInstance(ORDER_TYPE_RECEIVING))
        fragment.add(OrderListFragment.newInstance(ORDER_TYPE_COMPLETE))

//        mBinding.viewPage.offscreenPageLimit = 3
        mBinding.viewpage.adapter = ViewPager2Adapter(supportFragmentManager,lifecycle,fragment)
        mBinding.tabLayout.setSnapOnTabClick(true)
        mBinding.tabLayout.setViewPager2(mBinding.viewpage,titles)

        mBinding.viewpage.setCurrentItem(if(orderType == 4) 0 else (orderType ?: 0) + 1,false)

        while (isActive) {
            events.receive()
        }
    }

}